<template>
  <div class="container">
    <save-subsidy ref="saveSubsidy" @refresh="search"></save-subsidy>
    <a-card class="general-card" title="补贴周期维护">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="area" label="地区">
                  <a-input v-model="userStore.userInfo.user.areaName" disabled></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-button type="primary" @click="search">
                  <template #icon>
                    <icon-search />
                  </template>
                  {{ $t('searchTable.form.search') }}
                </a-button>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="16">
          <a-space>
            <a-button type="primary" @click="handleClickToSave()">
              <template #icon>
                <icon-plus />
              </template>
              {{ $t('searchTable.operation.create') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData.rows"
        :bordered="false"
        :pagination="{
          total: renderData.total,
          pageSize: formModel.rows,
          showTotal: true,
          showPageSize: true,
        }"
        @page-change="onPageChange"
        @page-size-change="onPageSizeChange"
      >
        <template #columns>
          <a-table-column title="序号" :width="60">
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 }}
            </template>
          </a-table-column>
          <a-table-column title="地区名称" data-index="areaName" :width="160" />
          <a-table-column title="周期名称" data-index="name" :width="160" />
          <a-table-column title="年份" data-index="year" :width="160" />
          <a-table-column title="养老补贴金额按月" data-index="yanglaofee" :width="160" />
          <a-table-column title="医疗补贴金额按月" data-index="yiliaofee" :width="160" />
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            fixed="right"
            data-index="operations"
            :width="160"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record)"
              >
                {{ $t('columns.operations.edit') }}
              </a-button>
<!--              <a-popconfirm-->
<!--                v-if="record.sysInnerFlag == 0"-->
<!--                :content="$t('columns.operations.delete')"-->
<!--                type="error"-->
<!--                @ok="handleClickToDel(record.id)"-->
<!--              >-->
<!--                <a-button type="text" size="small" status="danger">-->
<!--                  {{ $t('columns.operations.delete') }}-->
<!--                </a-button>-->
<!--              </a-popconfirm>-->
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import {
    UserVo,
    delUser,
  } from '@/api/user';
  import { Message } from '@arco-design/web-vue';
  import { TcCycleRes, tCyclePage, TCycleQueryDTO, TCycleVO } from "@/api/subsidy";
  import SaveSubsidy from "@/views/system/subsidy-conf/list/components/save-subsidy.vue";
  import { useUserStore } from "@/store";

  const userStore = useUserStore();
  const generateFormModel = () => {
    return {
      area: userStore.userInfo.user.area,
      page: 1,
      rows: 20,
    };
  };
  const { loading, setLoading } = useLoading();
  const renderData = ref<TcCycleRes>({
    rows: [],
    total: 0,
  }); // ref<PersonVo[]>([]);
  const formModel = ref(generateFormModel());
  const saveSubsidy = ref();
  const fetchData = async (
    params: TCycleQueryDTO // = { current: 1, pageSize: 20 }
  ) => {
    setLoading(true);
    try {
      const { data } = await tCyclePage(params);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const search = () => {
    fetchData(formModel.value as unknown as TCycleQueryDTO);
  };

  const handleClickToSave = (obj?: TCycleVO) => {
    saveSubsidy.value.handleChangeVisible(obj);
  };

  const handleClickToDel = (id: string) => {
    delUser(id).then(() => {
      Message.success('操作成功');
      search();
    });
  };

  const onPageChange = (current: number) => {
    formModel.value.page = current;
    search();
  };
  const onPageSizeChange = (current: number) => {
    formModel.value.rows = current;
    search();
  };

  search();
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
